﻿using de.wolfmarco.pas.Shared.Interfaces;
using de.wolfmarco.pas.Shared.MessageBoxes;
using de.wolfmarco.pas.Shared.Regions;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.Mvvm;
using Microsoft.Practices.Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace de.wolfmarco.pas.Shared.MVVM
{
    public class NavigateViewModel : ModuleViewModel
    {
        public NavigateViewModel(IRegionManager regionManager, IModuleManager moduleManager)
            : base(regionManager, moduleManager)
        {
            RequestNavigateCommand = new DelegateCommand<string>(OnTryRequestNavigate, CanRequestNavigate);

            ShowWindowCommand = new DelegateCommand<Type>(OnTryShowWindow, CanShowWindow);

            ShowEditWindowCommand = new DelegateCommand<EditWindowInfo>(OnTryShowEditWindow, CanShowEditWindow);
        }

        #region RequestNavigateCommand

        public DelegateCommand<string> RequestNavigateCommand { get; private set; }

        private void OnTryRequestNavigate(string viewName)
        {
            try
            {
                OnRequestNavigate(viewName);
            }
            catch (Exception ex)
            {
                PASMessageBox.ShowException(ex);
            }
        }

        protected virtual void OnRequestNavigate(string viewName)
        {
            if (viewName != null)
                regionManager.RequestNavigate(RegionNames.ModuleRegion, new Uri(viewName, UriKind.Relative));
        }

        protected virtual bool CanRequestNavigate(string viewName)
        {
            if (viewName != null)
            {
                // Exists a view with the class name in viewName?
                return regionManager.Regions[RegionNames.ModuleRegion].Views.SingleOrDefault(view => view.GetType().Name == viewName) != null ? true : false;
            }
            
            return false;
        }

        #endregion RequestNavigateCommand

        #region ShowWindowCommand

        public DelegateCommand<Type> ShowWindowCommand { get; private set; }

        private void OnTryShowWindow(Type window)
        {
            try
            {
                OnShowWindow(window);
            }
            catch (Exception ex)
            {
                PASMessageBox.ShowException(ex);
            }
        }

        protected virtual void OnShowWindow(Type window)
        {
        }

        protected virtual bool CanShowWindow(Type window)
        {
            return true;
        }

        #endregion ShowWindowCommand

        #region ShowEditWindowCommand

        public DelegateCommand<EditWindowInfo> ShowEditWindowCommand { get; private set; }

        private void OnTryShowEditWindow(EditWindowInfo editWindowInfo)
        {
            try
            {
                OnShowEditWindow(editWindowInfo);
            }
            catch (Exception ex)
            {
                PASMessageBox.ShowException(ex);
            }
        }

        protected virtual void OnShowEditWindow(EditWindowInfo editWindowInfo)
        {
            EditWindowView editWindow = new EditWindowView(editWindowInfo.CurrentContentControl);
            editWindow.Show();
        }

        protected virtual bool CanShowEditWindow(EditWindowInfo editWindowInfo)
        {
            return true;
        }

        #endregion ShowEditWindowCommand
    }
}
